package nl.hanze.designpatterns.Views.Login;

import nl.hanze.designpatterns.Controllers.LoginController;
import nl.hanze.designpatterns.GenerateLoginCredentialsSerialized;
import nl.hanze.designpatterns.GenerateQAObjSerialized;
import nl.hanze.designpatterns.GenerateTroubleTicketSerialized;
import nl.hanze.designpatterns.Models.MainModel;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/**
 *
 * Created by Aron on 23-9-2014.
 */
public class LoginScreen extends JPanel implements ActionListener, ItemListener {


    private JPanel loginPanel;
    private JLabel LoginLabel;
    private JPasswordField passwordField;
    private JTextField usernameField;
    private JLabel usernameLabel;
    private JButton loginButton;
    private JLabel passwordLabel;
    private JComboBox DAOFacComboBox;
    private JButton generateButton;
    private JLabel DAOImpl;

    private LoginController controller;
    private MainModel mainModel;

    public LoginScreen(LoginController controller, MainModel mainModel) {
        this.controller = controller;
        this.mainModel = mainModel;
        add(loginPanel);
        addListeners();
    }

    public void addListeners() {
        loginButton.setActionCommand("login");
        loginButton.addActionListener(this);
        usernameField.setActionCommand("login");
        usernameField.addActionListener(this);
        passwordField.setActionCommand("login");
        passwordField.addActionListener(this);
        DAOFacComboBox.setActionCommand("selectDAOFac");
        DAOFacComboBox.addItemListener(this);
        generateButton.setActionCommand("generate");
        generateButton.addActionListener(this);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        switch(e.getActionCommand())
        {
            case "login":
                System.out.println("[ACTION]login");
                this.controller.login(usernameField.getText(), passwordField.getText());
                break;
            case "generate":
                System.out.println("[ACTION]generate");
                GenerateQAObjSerialized.generate();
                GenerateLoginCredentialsSerialized.generate();
                GenerateTroubleTicketSerialized.generate();
                break;
            default:
                System.out.println("[ACTION]unknown: "+e.getActionCommand());
        }

    }

    @Override
    public void itemStateChanged(ItemEvent event) {
        if (event.getStateChange() == ItemEvent.SELECTED) {
            Object object = event.getItem();

            if (!(object instanceof String)) {
                return;
            }
            String item = (String) object;

            System.out.println("[ITEMEVENT]selectDAOFac "+item);
            this.controller.changeDAOFactory(item);
        }
    }
}
